﻿// +------------------------------------------------+---------------+
// | Automatic Checkbox Unchecker                   | Version 1.0.1 |
// +------------------------------------------------+---------------+
// | Automatically unchecks any listed checkbox present on          |
// | listed websites while running. This may be used to prevent     |
// | installing crapware bundled with webinstalls by known vendors. |
// +----------------------------------------------------------------+
// | Copyrright ® 2014 ŁSL Scripting.                               |
// | Redistribution and use in source and binary forms,             |
// | with or without modification, are permitted provided           |
// | that the following conditions are met:                         |
// | 1. Redistributions of source code must retain                  |
// |    the above copyright notice, this list of conditions         |
// |    and the following disclaimer.                               |
// | 2. Redistributions in binary form must reproduce               |
// |    the above copyright notice, this list of conditions         |
// |    and the following disclaimer in the documentation           |
// |    and/or other materials provided with the distribution.      |
// | 3. Neither the name of ŁSL Scripting, nor the names            |
// |    of its contributors may be used to endorse or promote       |
// |    products derived from this software without specific        |
// |    prior written permission.                                   |
// +----------------------------------------------------------------+
// | This software is provided by the copyright holders             |
// | and contributors "as is" and any express or implied warranties |
// | including, but not limited to, the implied warranties          |
// | of merchantability and fitness for a particular purpose        |
// | are disclaimed. In no event shall the copyright holder         |
// | or contributors be liable for any direct, indirect,            |
// | incidental, special, exemplary, or consequential damages       |
// | including, but not limited to, procurement of substitute goods |
// | or services, loss of use, data, or profits,                    |
// | or business interruption, however caused and on any theory     |
// | of liability, whether in contract, strict liability,           |
// | or tort (including negligence or otherwise) arising in any way |
// | out of the use of this software, even if advised               |
// | of the possibility of such damage.                             |
// +----------------------------------------------------------------+
// ==UserScript==
// @name          Automatic Checkbox Unchecker
// @namespace     code.google.com/p/auto-checkbox-unchecker
// @description   Automatically unchecks any listed checkbox present on listed websites while running. This may be used to prevent installing crapware bundled with webinstalls by known vendors.
// @downloadURL   https://auto-checkbox-unchecker.googlecode.com/svn/stable/latest/auto_checkbox_unchecker.user.js
// @updateURL     https://auto-checkbox-unchecker.googlecode.com/svn/stable/latest/auto_checkbox_unchecker.meta.js
// @version       1.0.1
// @match         *://get.adobe.com/*/flashplayer*
// ==/UserScript==
// +----------------------------------------------------------------+


// ---- CONFIGURATION ----
var filterDatabase = new Array(
							   new Array('get.adobe.com', 'input#offerCheckbox')
							   )


// ---- MATCHING LIBRARY ----
// getElementsBySelector() function implementation - written by folks at openjs.com, thanks for a very good function (:
if ((typeof getElementsBySelector) == "undefined") {
  window.getElementsBySelector = function getElementsBySelector(allSelectors) {
		var selected = new Array();
		if(!document.getElementsByTagName) {
			return selected;
		}
		allSelectors = allSelectors.replace(/\s*([^\w])\s*/g,"$1");
		var selectors = allSelectors.split(",");
		var getElements = function(context,tag) {
			if (!tag) {
				tag = '*';
			}
			var found = new Array;
			for (var a=0,len=context.length; con=context[a],a<len; a++) {
				var eles;
				if (tag == '*') {
					eles = con.all ? con.all : con.getElementsByTagName("*");
				}
				else {
					eles = con.getElementsByTagName(tag);
				}
			for(b=0,leng=eles.length;b<leng; b++) {
				found.push(eles[b]);
			}
		}
		return found;
	}
	COMMA:
	for(i=0,len1=selectors.length; selector=selectors[i],i<len1; i++) {
		var context = new Array(document);
		var inheriters = selector.split(" ");
		SPACE:
		for(j=0,len2=inheriters.length; element=inheriters[j],j<len2;j++) {
			var leftBracket = element.indexOf("[");
			var rightBracket = element.indexOf("]");
			var pos = element.indexOf("#");
			if(pos+1 && !(pos>leftBracket&&pos<rightBracket)) {
				var parts = element.split("#");
				var tag = parts[0];
				var id = parts[1];
				var ele = document.getElementById(id);
				if(!ele || (tag && ele.nodeName.toLowerCase() != tag)) {
					continue COMMA;
				}
				context = new Array(ele);
				continue SPACE;
			}
			pos = element.indexOf(".");
			if(pos+1 && !(pos>leftBracket&&pos<rightBracket)) {
				var parts = element.split('.');
				var tag = parts[0];
				var class_name = parts[1];
				var found = getElements(context,tag);
				context = new Array;
 				for (var l=0,len=found.length; fnd=found[l],l<len; l++) {
 					if(fnd.className && fnd.className.match(new RegExp('(^|\s)'+class_name+'(\s|$)'))) context.push(fnd);
 				}
				continue SPACE;
			}
			if(element.indexOf('[')+1) {
				if (element.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?['"]?([^\]'"]*)['"]?\]$/)) {
					var tag = RegExp.$1;
					var attr = RegExp.$2;
					var operator = RegExp.$3;
					var value = RegExp.$4;
				}
				var found = getElements(context,tag);
				context = new Array;
				for (var l=0,len=found.length; fnd=found[l],l<len; l++) {
 					if(operator=='=' && fnd.getAttribute(attr) != value) continue;
					if(operator=='~' && !fnd.getAttribute(attr).match(new RegExp('(^|\\s)'+value+'(\\s|$)'))) continue;
					if(operator=='|' && !fnd.getAttribute(attr).match(new RegExp('^'+value+'-?'))) continue;
					if(operator=='^' && fnd.getAttribute(attr).indexOf(value)!=0) continue;
					if(operator=='$' && fnd.getAttribute(attr).lastIndexOf(value)!=(fnd.getAttribute(attr).length-value.length)) continue;
					if(operator=='*' && !(fnd.getAttribute(attr).indexOf(value)+1)) continue;
					else if(!fnd.getAttribute(attr)) continue;
					context.push(fnd);
 				}
				continue SPACE;
			}
			var found = getElements(context,element);
			context = found;
		}
		for (var o=0,len=context.length;o<len; o++) selected.push(context[o]);
	}
	return selected;
}
}

// ---- MAIN CODE ----
// it'll escape through XPC API, so it operates on actual site (:
window.uncheckCheckboxes = function uncheckCheckboxes() {
	for (iterator = 0; iterator < filterDatabase.length; iterator++) {
		if (window.location.href.match(filterDatabase[iterator][0])) {
			var checkboxes = getElementsBySelector(filterDatabase[iterator][1]);
			if (checkboxes.length > 0) {
				for (checkbox = 0; checkbox < checkboxes.length; checkbox++) {
					checkboxes[checkbox].checked = false;
				}
			}
		}
	}
}

uncheckCheckboxes();
window.setTimeout("uncheckCheckboxes();", 1000);